<script setup>
import { showSuccessToast, showFailToast } from 'vant'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/stores/modules/user'
import { submitNextLevel } from '@/api/jdb'

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  phone: undefined,
  name: undefined,
})
const submitLoading = ref(false)

const onSubmit = () => {
  if(!form.value.phone || form.value.phone.length !== 11 || !(/^1[3456789]\d{9}$/.test(form.value.phone))) {
    showFailToast('手机号格式错误')
    return
  }

  submitLoading.value = true

  submitNextLevel({
    phone: form.value.phone,
    name: form.value.name,
  }).then((response) => {
    showSuccessToast('邀请成功')
    router.replace({path: "/we-chat-service-account"})
  }).finally(() => submitLoading.value = false)
}

const init = () => {

}
init()
</script>

<template>
  <header>
    <div class="header">加入驾多宝，工资涨不停</div>
  </header>
  <van-form @submit="onSubmit" class="form">
    <div class="van-hairline--top"></div>
    <van-cell-group>
      <van-field
          :label-width="'90px'"
          size="large"
          style="background: none;font-size:16px;"
          center
          v-model="form.phone"
          name="手机号"
          placeholder="请输入手机号码"
      >
        <template #label>
          <div style="width: 100%;text-align: right;color:#32434C;font-size:16px;">手机号：</div>
        </template>
      </van-field>
    </van-cell-group>
    <div class="van-hairline--top"></div>
    <van-cell-group>
      <van-field
          :label-width="'90px'"
          size="large"
          center
          style="background: none;font-size:16px;"
          v-model="form.name"
          name="名称"
          placeholder="请输入驾校名称或教练姓名"
      >
        <template #label>
          <div style="width: 100%;text-align: right;color:#32434C;font-size:16px;">名称：</div>
        </template>
      </van-field>
    </van-cell-group>
    <div class="van-hairline--top"></div>
    <van-button
        style="margin:80px auto 0;font-size:18px;border-radius: 5px;width:330px"
        block
        type="primary"
        native-type="submit"
        color="#04be02"
        :loading="submitLoading"
        loading-text="加载中..."
    >
      提交
    </van-button>
  </van-form>
</template>

<style scoped>
.header {
  text-align: center;
  font-size: 19px;
  color: #3cc51f;
  font-weight: 400;
  margin: 50px auto;
}
:deep(.van-field__control::-webkit-input-placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field__control::placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field) {
  padding-left:0;
  padding-right: 0;
}
.form-cell-group :deep(.van-cell) {
  padding-left: 0;
  padding-right: 0;
  margin: 0 auto;
  height: 60px;
}
</style>
